package com.nkzy.service.impl;

import com.nkzy.mapper.UserMapper;
import com.nkzy.pojo.Article;
import com.nkzy.pojo.User;
import com.nkzy.service.UserService;
import com.nkzy.utils.Md5Util;
import com.nkzy.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User findUserByName(String username) {
        User user = userMapper.selectUserByName(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        //1.对密码进行加密
        String md5String = Md5Util.getMD5String(password);
        //2.注册
        userMapper.insert02(username,md5String);
    }

    @Override
    public void update(User user) {
        //要把今天的日期保存到表中的update_time，在业务层要完成这项工作
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //在业务层从ThreadLocal对象中取出ID,d Mapper更新和使用id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);


    }

    @Override
    public void updatePwd(String newPwd) {
        //在业务层要完成业务逻辑，修改谁的密码？
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePWd(Md5Util.getMD5String(newPwd),id);

    }
}



























